package cn.bugstack.design;

import cn.bugstack.design.event.EventManager;
import cn.bugstack.design.event.listener.MQEventListener;
import cn.bugstack.design.event.listener.MessageEventListener;

/**
 *
 */
public abstract class LotteryService {

    private EventManager eventManager;

    /**
     * 在LotteryService构造器里面，直接new了一个EventManager？这不就耦合了吗？？？
     * 以后多一种通知方式，就要修改这里的构造器传参，不好吧？
     * 能否通过Spring来管理bean，从而解耦。
     */
    public LotteryService() {
        eventManager = new EventManager(EventManager.EventType.MQ, EventManager.EventType.Message);
        eventManager.subscribe(EventManager.EventType.MQ, new MQEventListener());
        eventManager.subscribe(EventManager.EventType.Message, new MessageEventListener());
    }

    public LotteryResult draw(String uId) {
        //先执行核心业务
        LotteryResult lotteryResult = doDraw(uId);
        //再通知1
        eventManager.notify(EventManager.EventType.MQ, lotteryResult);
        //再通知2
        eventManager.notify(EventManager.EventType.Message, lotteryResult);

        return lotteryResult;
    }

    //核心业务
    protected abstract LotteryResult doDraw(String uId);

}
